package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import business.Utilisateur;

import com.cedarsoftware.util.io.JsonWriter;

import dao.DaoAmitie;


public class S_Friend extends HttpServlet {
	private static final long serialVersionUID = 1L;


	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("json");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		int id_utilisateur = ((Utilisateur) request.getSession().getAttribute("utilisateur")).getId_utilisateur();
		
		// Renvoie la liste des amis.
		List<Utilisateur> friends = DaoAmitie.getAmis(id_utilisateur);
		if(friends != null && !friends.isEmpty())
			out.print(JsonWriter.objectToJson(friends));
		else
			out.print("{}");
		
		out.close();
	}
}